// pages/my/President/President.js
import { Api } from '../../../utils/api.js';
const con = require("../../../utils/data.js");
const api = new Api();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 登录表单相关数据
    fTellphone: '',
    fPassword: '',
    fsid:'',
    psw_text:'',
    tel_text:''
  },
  ljzc(){
    wx.navigateTo({
      url: "zhuce/zhuce"
    })
  },
  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },
  login(){
    wx.showToast({
      title: '登录成功',
      icon: 'success',
      duration: 2000
    }),
    wx.navigateTo({
      url: "/pages/merchant/merchant"
    })
  },
  //  表单验证
  loginSubmit(e) {
    var that=this;
    if(that.data.isAgree==true){
      wx.setStorageSync('psd', e.detail.value.fPassword);
      wx.setStorageSync('tel', e.detail.value.fTellphone)
    }
    console.log(e);
    let fTellphone = e.detail.value.fTellphone;
    let fPassword = e.detail.value.fPassword;
    if (fTellphone == '') {
      api.showToast('请输入手机号');
      return;
    } 
    if (fTellphone.length !== 11) {
      api.showToast('手机号格式错误');
      return;
    }
    if (fPassword == '') {
      api.showToast('请输入登录密码');
      return;
    }
    else{
      // 提交表单
      wx.request({
        url: con.index_clogin,
        // url:'http://192.168.0.105:8080/certification/login',
        data: {
          'fTellphone': e.detail.value.fTellphone,
          'fPassword': e.detail.value.fPassword
        },
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res.data);
          console.log('返回成功');
          that.setData({
            'fsid':res.data.body.fSid,
          });
          wx.setStorageSync('invalidCommission', res.data.body.invalidCommission.toFixed(2))
          wx.setStorageSync('validCommission', res.data.body.validCommission.toFixed(2))
          wx.setStorageSync('todayEarnings', res.data.body.todayEarnings.toFixed(2))
          wx.setStorageSync('promoCode', res.data.body.promoCode)
          wx.setStorageSync('fSellname', res.data.body.fSellname)
          wx.setStorageSync('fSid', res.data.body.fSid)
          wx.setStorageSync('discount', res.data.body.discount)
          var fsid = res.data.body.fSid;
          if(res.data.status==true){
          wx.showToast({
            title: '登录成功',
            image: '/images/icon/success.png',
            duration: 2000
          })
           wx.navigateTo({
             url: "/pages/merchant/merchant?fsid="+fsid,
          })
          }
          if(res.data.status==false){
            if(res.data.message=='手机号未找到'){
            wx.showToast({
              title: '手机号未找到',
              image: '/images/icon/fail.png',
              duration: 1000
              })}
            if (res.data.message == '正在路上') {
              wx.showToast({
                title: '手机号未找到',
                image: '/images/icon/fail.png',
                duration: 1000
              })
            }
            else{
              wx.showToast({
                title: '密码错误',
                image: '/images/icon/fail.png',
                duration: 1000
              })
            }
          }
        },
        fail: function (res) {
          console.log('返回失败');
        }
      })    
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync('psd').length>0){
      this.setData({
        'isAgree': true,
        'psd_text': wx.getStorageSync('psd'),
        'tel_text': wx.getStorageSync('tel'),
      })
     
    }
    else{
      this.setData({
        'isAgree': false,
      })

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})